/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamen.gesplan.vistes;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
/**
 *
 * @author Roger
 */
public class prova extends javax.swing.JFrame {

    private boolean newInfo = false;
    private boolean closed = false;
    private String[] llista;
    private Object[] selected;
    
    
    public prova() {
        initComponents();
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension finestra = getSize();
        this.setLocation((pantalla.width - finestra.width) / 2,(pantalla.height - finestra.height) / 2);
        setVisible(true);
        setResizable(false);
        this.setTitle("Esborrar empleat");
        
        //buscador2.addKeyListener(this);
    }
    
    
    public boolean newInfo() {
        return newInfo;
    }

    public void setNewInfo(boolean info) {
        this.newInfo = info;
    }

    public boolean isClosed() {
        return closed;
    }

    public void setClosed(boolean closed) {
        this.closed = closed;
    }
    
    public String getComments() {
        return comments.getText();
    }

   /* public Date getDate_end() {
        return DateParser(date_end.getText());
    }

    public Date getDate_start() {
        return DateParser(date_start.getText());
    }*/

    public String getStaffName() {
        return name.getText();
    }

    public String getRol() {
        return (String) comborol.getItemAt(comborol.getSelectedIndex());
    }

    public String getSection() {
        return (String) comboseccio.getItemAt(comboseccio.getSelectedIndex());
    }

    public String getSurname() {
        return surname.getText();
    }


    public void setModelRol(DefaultComboBoxModel modelRol) {
        comborol.setModel(modelRol);
    }

    public void setModelSection(DefaultComboBoxModel modelSection) {
        comboseccio.setModel(modelSection);
    }

    public Date getDate_start() {
        return dataentrada.getDate();
    }

    public Date getDate_end() {
        return datasortida.getDate();
    }
    
    public void setList(DefaultComboBoxModel model) {
        
        /* El mètode de domini ens retorna un 
         * DefaultComboBoxModel, però el component necessita
         * les dades com a String[], així que fem la conversió
         */
        
        llista = new String[model.getSize()];
        for (int i=0; i < model.getSize(); ++i) {
            llista[i] = (String) model.getElementAt(i);
        }
        buscador.establecerLista(llista);
    }
    
    public void setJList(DefaultComboBoxModel model) {
        
        /* El mètode de domini ens retorna un 
         * DefaultComboBoxModel, però el component necessita
         * les dades com a String[], així que fem la conversió
         */
        
        llista = new String[model.getSize()];
        for (int i=0; i < model.getSize(); ++i) {
            llista[i] = (String) model.getElementAt(i);
        }
        llista_possibles.setModel(model);
    }

    public String getSelected() {
        return buscador.getText();
    }

    public String[] getListSelected() {
        String[] res = new String[selected.length];
        for (int i=0; i < res.length; ++i) {
            res[i] = selected[i].toString();
        }
        return res;
    }
    
    
    
    public void infoMessage(int res) {
        if (res == 0) {
            JOptionPane.showMessageDialog(this, "No existeix aquest empleat", 
                    "Error d'eliminació", JOptionPane.ERROR_MESSAGE);
        }
        else {
            JOptionPane.showMessageDialog(this, "S'han eliminat " + res + " empleats", 
                    "Eliminació confirmada", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane2 = new javax.swing.JTabbedPane();
        jRadioButton1 = new javax.swing.JRadioButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        datasortida = new com.toedter.calendar.JDateChooser();
        comboseccio = new javax.swing.JComboBox();
        dataentrada = new com.toedter.calendar.JDateChooser();
        b_submit = new javax.swing.JButton();
        comborol = new javax.swing.JComboBox();
        name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        comments = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        surname = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        buscador2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        buscador = new LuisPlugin.LTextField();
        esborrar = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        llista_possibles = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();

        jRadioButton1.setText("jRadioButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        comboseccio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Carregant Seccions" }));

        b_submit.setText("Afegir");
        b_submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_submitActionPerformed(evt);
            }
        });

        comborol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Carregant Rols" }));

        jLabel2.setText("Cognom");

        jLabel1.setText("Nom");

        comments.setColumns(20);
        comments.setRows(5);
        jScrollPane1.setViewportView(comments);

        jLabel5.setText("Rol ID");

        jLabel4.setText("Data sortida");

        jLabel3.setText("Data entrada");

        jLabel7.setText("Comentaris");

        jLabel6.setText("Secció ID");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(b_submit))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane1)
                                    .addComponent(comboseccio, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(comborol, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(datasortida, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(surname)
                                    .addComponent(name)
                                    .addComponent(dataentrada, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                .addContainerGap(246, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(17, 17, 17)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(surname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addComponent(dataentrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(datasortida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comborol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(57, 57, 57)
                        .addComponent(jLabel7)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(comboseccio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(b_submit)
                .addGap(16, 16, 16))
        );

        jTabbedPane1.addTab("Afegir empleat", jPanel1);

        jLabel8.setText("Busca per cognom:");

        esborrar.setText("Esborra");
        esborrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                esborrarActionPerformed(evt);
            }
        });

        jLabel9.setText("Busca per cognom:");

        jScrollPane2.setViewportView(llista_possibles);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(buscador2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                    .addComponent(esborrar)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(buscador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(253, 253, 253))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buscador2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(esborrar)
                .addGap(37, 37, 37))
        );

        jTabbedPane1.addTab("Esborrar Empleat", jPanel2);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 547, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 390, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Altres", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_submitActionPerformed
        newInfo = true;
        /*
         * Comprovem dates Date d = DateParser(date_start.getText()); Date d1 =
         * DateParser(date_end.getText()); if (d != null && d.equals(new
         * Date(-1))) newInfo = false; if (d1 != null && d1.equals(new
         * Date(-1))) newInfo = false;
         */
    }//GEN-LAST:event_b_submitActionPerformed

    private void esborrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esborrarActionPerformed
        Object[] opcions = {"Sí", "No"};

        /*
         * Demanem confirmació per esborrar, 0 = Sí, 1 = No
         */
        int res = JOptionPane.showOptionDialog(this, "Segur que vols esborrar l'empleat "
                + buscador.getText() + "?", "Confirma eliminació", JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.WARNING_MESSAGE, null, opcions, opcions[1]);

        if (res == 0) {
            newInfo = true;

            selected = llista_possibles.getSelectedValues();

            /*
             * tot aquest codi de sota es pel buscador1, amb el buscador2
             * actualitzem la llista cridant a BD
             */

            /*
             * Ara hem d'actualitzar la llista
             */
            List newList = new ArrayList();
            /*
             * Creem una llista auxiliar on estan tots els elemnents menys
             * l'esborrat
             */
            for (int i = 0; i < llista.length; ++i) {
                if (!llista[i].equals(buscador.getText())) {
                    newList.add(llista[i]);
                }
            }
            /*
             * Fem la còpia a la llista original
             */
            llista = new String[newList.size()];
            for (int i = 0; i < llista.length; ++i) {
                llista[i] = (String) newList.get(i);
            }
            /*
             * Establim la llista
             */
            buscador.establecerLista(llista);
        } else {
            newInfo = false;
        }

    }//GEN-LAST:event_esborrarActionPerformed

    public void keyTyped(KeyEvent e) {
        
    }

    public void keyPressed(KeyEvent e) {
        
    }

    public void keyReleased(KeyEvent e) {
        if (e.getSource()==buscador2) {
            String ini = buscador2.getText();
            DefaultListModel results = new DefaultListModel() {};
            for (int i=0; i < llista.length; ++i) {
                if (llista[i].startsWith(ini)) results.addElement(llista[i]);
            }
            llista_possibles.setModel(results);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_submit;
    private LuisPlugin.LTextField buscador;
    private javax.swing.JTextField buscador2;
    private javax.swing.JComboBox comborol;
    private javax.swing.JComboBox comboseccio;
    private javax.swing.JTextArea comments;
    private com.toedter.calendar.JDateChooser dataentrada;
    private com.toedter.calendar.JDateChooser datasortida;
    private javax.swing.JButton esborrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JList llista_possibles;
    private javax.swing.JTextField name;
    private javax.swing.JTextField surname;
    // End of variables declaration//GEN-END:variables
}
